package com.itheima.web; /**
 * @author Xiao
 * @date 2021/8/31
 */

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/selectAllServletJson")
public class SelectAllServletJson extends HttpServlet {
    private BrandService brandService = new BrandService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //查询数据
        List<Brand> brands = brandService.selectAll();
        //转为JSON字符串 --》 序列化
        String toJSONString = JSON.toJSONString(brands);
        //响应数据
        response.setContentType("text/json;charset=utf-8"); //转码
        response.getWriter().write(toJSONString);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
